package org.teremail.client;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.TextArea;

public class MessageTextController {

    private final TextArea view;
    private final MailServiceAsync service;
    private final ErrorHandler eh;
    private String messageText;
    private String folder;
    private int uid;

    public MessageTextController(TextArea view, ErrorHandler eh, MailServiceAsync service) {
        this.view = view;
        this.service = service;
        this.eh = eh;
        
        init();
    }
    
    private void init() {
        view.setWidth("100%");
        view.setHeight("25em");
        view.addStyleName("bordered");
        
        setMessageText("");
    }
    
    private void setMessageText(String message) {
        this.messageText = message;
        
        view.setText(message);
    }
    
    public void fetchMessage(String mailbox, String folderName, int uid) {
        this.folder = folderName;
        this.uid = uid;
        
        service.getMessage(mailbox, folder, uid, new AsyncCallback<String>() {

            public void onFailure(Throwable caught) {
                eh.showMessage(caught.getMessage());
            }

            public void onSuccess(String result) {
                setMessageText(result);
            }
            
        });
    }
}
